/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.core.domainmodel.events;

import java.io.Serializable;

/**
 * Event sent by broker to confirm order execution (complete or partial).
 * If execution fills the order, it moves to the terminal state (closed).<br/>
 * An OrderEvent subclass. Holds the following associated variable:
 * <ul>
 * <li>quantity(double)</li>
 * <li>price(double)</li>
 * <li>commission(double)</li>
 * <li>cumulatedQuantity(double)</li>
 * <li>averagePrice(double)</li>
 * </ul>
 * And these inherited variables:
 * <ul>
 * <li>id(Long)</li>
 * <li>eventTimeStamp(TimeStamp)</li>
 * <li>message(String)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [11.12.2007] Created (Mike)<br>
 *
 *  @author Mike
 */
public class OrderExecutionEvent extends OrderEvent implements Serializable {
	
	private static final long serialVersionUID = -5261131790980157322L;
	/**
	 * private double quantity = 0.0;
	 */
	private double quantity = 0.0; 
	/**
	 * private double price = 0.0;
	 */
	private double price = 0.0;
	/**
	 * private double commission = 0.0;
	 */
	private double commission = 0.0; // FIXME: can commission be in different currency that the order? - Ulrich says: It can, but it is always in the account currency as far as i know.
	
	/**
	 * private double cumulatedQuantity = 0.0;<br/>
	 * we get this from IB's Execution object, so lets use it as another way to check if a need to throw an OrderCompletitionEvent (in case I restarted and lost the openQuantity amount) 
	 */
	private double cumulatedQuantity = 0.0; 
	/**
	 * private double averagePrice = 0.0;<br/>
	 * we get this from IB's Execution object
	 */
	private double averagePrice = 0.0;
	
	/**
	 * How many contracts were executed (bought/sold).<br/>
	 * returns the associated quantity(double)
	 * @return quantity.
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * Sets execution quantity.<br/>
	 * sets the associated quantity(double) with the given val(double)
	 * @param val quantity.
	 */
	public void setQuantity(double val) {
		this.quantity = val;
	}

	/**
	 * What was the price of transaction.<br/>
	 * returns the associated price(double)
	 * @return price.
	 */
	public double getPrice() {
		return price;
	}
	
	/**
	 * Sets the price.<br/>
	 * sets the associated price(double) with the given executionPrice(double)
	 * @param executionPrice price.
	 */

	public void setPrice(double executionPrice) {
		this.price = executionPrice;
	}

	/**
	 * Commission charged by the broker for this transaction.<br/>
	 * returns the associated commission(double)
	 * @return commission.
	 */
	public double getCommission() {
		return commission;
	}

	/**
	 * Sets commission.<br/>
	 * sets the associated commission(double) with the given commission(double)
	 * @param commission commission.
	 */
	public void setCommission(double commission) {
		this.commission = commission;
	}
	/**
	 * returns the associated cumulatedQuantity(double)<br/>
	 * This is the total quantity filled by the Order so far (including this execution)
	 * @return
	 */
	public double getCumulatedQuantity() {
		return cumulatedQuantity;
	}
	/**
	 * sets the associated cumulatedQuantity(double) with the given cumulatedQuantity(double)
	 * @param cumulatedQuantity
	 */
	public void setCumulatedQuantity(double cumulatedQuantity) {
		this.cumulatedQuantity = cumulatedQuantity;
	}
	/**
	 * returns the associated averagePrice(double)<br/>
	 * This is the average price obtained by the Order so far (up to this execution, without the commission)
	 * @return
	 */
	public double getAveragePrice() {
		return averagePrice;
	}
	/**
	 * sets the associated averagePrice(double) with the given averagePrice(double)
	 * @param averagePrice
	 */
	public void setAveragePrice(double averagePrice) {
		this.averagePrice = averagePrice;
	}

	/**
	 * returns a String in the form: "OrderExecutionEvent: eventTimeStamp=${eventTimeStamp}, message=${message}, quantity=${quantity}, price=${price}, commission=${commission}"
	 */
	public String toString() {
		return "OrderExecutionEvent: eventTimeStamp=" + getEventTimeStamp()
		+ ", message=" + getMessage()
		+ ", quantity=" + getQuantity()
		+ ", price=" + getPrice()
		//+ ", commission=" + getCommission() always 0 with IB, since these come with a separate openOder call
		+ ", avgPrice=" + getAveragePrice()
		+ ", cumQuantity=" + getCumulatedQuantity();
	}
}